<?php

namespace App\Service\Fdd;

use App\Service\Fdd\FddClient;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;

class FddService implements FddInterface
{
    private FddClient $client;

    public function __construct(FddClient $client)
    {
        $this->client = $client;
    }

    # 获取服务访问凭证
    public function getAccessToken()
    {
        $token = Cache::get('FDD_ACCESS_TOKEN');
        if ($token) {
            return $token;
        }
        $result = $this->client->request(null, null, FddConstants::SERVICE_GET_ACCESS_TOKEN);
        Cache::put('FDD_ACCESS_TOKEN', $result['accessToken'], 3600);
        return $result['accessToken'];
    }

    # 获取应用级资源访问凭证
    function getAppAccessTicket()
    {
        return $this->client->request(null, null, FddConstants::SERVICE_GET_APP_ACCESS_TICKET);
    }

    # 获取用户级资源访问凭证
    function getUserAccessTicket()
    {
        return $this->client->request(null, null, FddConstants::SERVICE_GET_USER_ACCESS_TICKET);
    }

    # 获取个人认证授权链接
    public function getUserAuthUrl(array $data)
    {
        $bizContent = json_encode($data);
        return $this->client->request($this->getAccessToken(), $bizContent, FddConstants::USER_GET_AUTH_URL);
    }

    public function getUserInfo($clientUserId)
    {
        return $this->client->request($this->getAccessToken(), json_encode(['clientUserId' => $clientUserId]), FddConstants::USER_GET_DETAIL);
    }

    public function getCorpInfo($clientUserId)
    {
        return $this->client->request($this->getAccessToken(), json_encode(['clientCorpId' => $clientUserId]), FddConstants::CORP_GET_DETAIL);
    }

    # 获取企业认证授权链接
    public function getCorpAuthUrl(array $data)
    {
        $bizContent = json_encode($data);
        return $this->client->request($this->getAccessToken(), $bizContent, FddConstants::CORP_GET_AUTH_URL);
    }

    # 获取模板列表
    public function getSignTemplateList(array $data)
    {
        return $this->client->request($this->getAccessToken(), json_encode($data), FddConstants::SIGN_TEMPLATE_GET_LIST);
    }

    # 获取模板详情
    public function getSignTemplateDetail($id)
    {
        return $this->client->request($this->getAccessToken(), json_encode(['signTemplateId' => $id]), FddConstants::SIGN_TEMPLATE_GET_DETAIL);
    }

    # 创建签署任务
    public function createSignTaskWithTemplate(array $data)
    {
        return $this->client->request($this->getAccessToken(), json_encode($data), FddConstants::SIGN_TASK_CREATE_WITH_TEMPLATE);
    }

    # 填写合同内容
    public function signTaskFillValues($data)
    {
        return $this->client->request($this->getAccessToken(), json_encode($data), FddConstants::SIGN_TASK_FILL_FIELDS_VALUE);
    }

    # 提交合同任务
    public function signTaskStart($id)
    {
        return $this->client->request($this->getAccessToken(), json_encode(['signTaskId' => $id]), FddConstants::SIGN_TASK_START);
    }

    # 获取参与方签署链接
    public function signTaskGetUrl($data)
    {
        return $this->client->request($this->getAccessToken(), json_encode($data), FddConstants::SIGN_TASK_ACTOR_GET_URL);
    }

    # 获取合同下载链接
    public function signTaskGetDownloadUrl($data)
    {
        Log::error('debug', $data);
        return $this->client->request($this->getAccessToken(), json_encode($data), FddConstants::SIGN_TASK_OWNER_GET_DOWNLOAD_URL);
    }

    # 获取签署任务预览链接
    public function signTaskGetPreviewUrl($data)
    {
        return $this->client->request($this->getAccessToken(), json_encode($data), FddConstants::SIGN_TASK_GET_PREVIEW_URL);
    }

    # 删除签署任务
    public function signTaskDelete($id)
    {
        return $this->client->request($this->getAccessToken(), json_encode(['signTaskId' => $id]), FddConstants::SIGN_TASK_DELETE);
    }

    # 合同定稿
    public function signTaskFinalize($id)
    {
        return $this->client->request($this->getAccessToken(), json_encode(['signTaskId' => $id]), FddConstants::SIGN_TASK_DOC_FINALIZE);
    }

    # 合同撤销
    public function signTaskCancel($id)
    {
        return $this->client->request($this->getAccessToken(), json_encode(['signTaskId' => $id, 'terminationNote' => '合同删除']), FddConstants::SIGN_TASK_CANCEL);
    }

    # 个人免签授权链接
    public function getPersonalFreeSignUrl($data)
    {
        return $this->client->request($this->getAccessToken(), json_encode($data), FddConstants::PERSONAL_SEAL_FREE_SIGN_GET_URL);
    }

    # 企业印章免签授权链接
    public function getCorpFreeSignUrl($data)
    {
        return $this->client->request($this->getAccessToken(), json_encode($data), FddConstants::SEAL_FREE_SIGN_GET_URL);
    }

    # 获取个人签名列表
    public function getPersonalSealList($data)
    {
        return $this->client->request($this->getAccessToken(), json_encode($data), FddConstants::PERSONAL_SEAL_GET_LIST);
    }

    # 获取企业印章列表
    public function getCorpSealList($data)
    {
        return $this->client->request($this->getAccessToken(), json_encode($data), FddConstants::SEAL_GET_LIST);
    }

    public function userUnbind($id)
    {
        return $this->client->request($this->getAccessToken(), json_encode(['openUserId' => $id]), FddConstants::USER_UNBIND);
    }
}
